import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class BlockRefreshWithBloc extends StatefulWidget {
  const BlockRefreshWithBloc({Key? key}) : super(key: key);

  @override
  _BlockRefreshWithBlocState createState() => _BlockRefreshWithBlocState();
}

class _BlockRefreshWithBlocState extends State<BlockRefreshWithBloc> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BlocProvider(
        create: (BuildContext context) {
          return PositionBloc();
        },
        child: BlocBuilder<PositionBloc, PosotionState>(
          builder: (BuildContext context, PosotionState state) {
            return Scaffold(
              appBar: AppBar(
                title: Text("Bloc局部刷新"),
                centerTitle: true,
              ),
              body: Container(
                child: Row(
                  children: [
                    Container(
                      color: Colors.grey,
                      width: 100,
                      child: LeftMenuWidget(),
                    ),
                    Expanded(child: RightMenu())
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}

enum PositionEvent { CHANGE_POS }

class PosotionState {
  int pos;

  PosotionState(this.pos);
}

class PositionBloc extends Bloc<PositionEvent, PosotionState> {
  int currentIndex = 0;

  PositionBloc() : super(PosotionState(0));

  void changePos(int index) {
    currentIndex = index;
    add(PositionEvent.CHANGE_POS);
  }

  @override
  Stream<PosotionState> mapEventToState(event) async* {
    if (event == PositionEvent.CHANGE_POS) {
      yield PosotionState(currentIndex);
    }
  }
}

class LeftMenuWidget extends StatefulWidget {
  const LeftMenuWidget({Key? key}) : super(key: key);

  @override
  _LeftMenuWidgetState createState() => _LeftMenuWidgetState();
}

class _LeftMenuWidgetState extends State<LeftMenuWidget> {
  List<String> list = ["菜单1", "菜单2", "菜单3"];

  @override
  Widget build(BuildContext context) {
    // PositionBloc bloc = context.read<PositionBloc>();
    PositionBloc bloc = BlocProvider.of<PositionBloc>(context);
    return Container(
      child: ListView.separated(
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(
                list[index],
                style: Theme.of(context).textTheme.bodyText1,
              ),
              onTap: () {
                bloc.changePos(index);
              },
            );
          },
          separatorBuilder: (context, index) {
            return Divider();
          },
          itemCount: list.length),
    );
  }
}

class RightMenu extends StatefulWidget {
  const RightMenu({Key? key}) : super(key: key);

  @override
  _RightMenuState createState() => _RightMenuState();
}

class _RightMenuState extends State<RightMenu> {
  @override
  Widget build(BuildContext context) {
    PosotionState state = context.read<PositionBloc>().state;
    return Container(
      child: Center(
        child: Text("${state.pos}"),
      ),
    );
  }
}
